package cn.com.bluemoon.daps.api.model.factory;

import cn.com.bluemoon.daps.api.model.RemoteDataModelService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.FeignClientTools;
import cn.com.bluemoon.daps.model.dto.ModelInfos;
import cn.com.bluemoon.daps.model.entity.DapDataModel;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class RemoteDataModelServiceFallbackFactory implements FallbackFactory<RemoteDataModelService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteDataModelServiceFallbackFactory.class);

    @Override
    public RemoteDataModelService create(Throwable throwable) {
        log.error("模型信息服务调用失败:{}", throwable.getMessage());
        return new RemoteDataModelService() {

            @Override
            public ResultBean<DapDataModel> getById(String id) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取单个模型失败");
            }

            @Override
            public List<DapDataModel> findBySystemId(String systemId) {
                return Collections.emptyList();
            }

            @Override
            public ResultBean<List<DapDataModel>> getModelList(String sysId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取模型列表失败");
            }

            @Override
            public ResultBean<List<DapDataModel>> getModels(String sysId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取模型列表失败");
            }

            @Override
            public ResultBean<List<DapDataModel>> getBatchModel(List<String> systemIds) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取模型列表失败");
            }

            @Override
            public ResultBean<ModelInfos> queryModelInfosByModelAndNewest(String modelId, Boolean newest) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "并行获取模型下的表、字段、索引、分区信息失败");
            }

            @Override
            public ResultBean<ModelInfos> queryModelInfosByModelAndNewestAndTableIds(String modelId, Boolean newest, List<String> tableIds) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "并行获取模型下的表、字段、索引、分区信息失败");
            }

            @Override
            public ResultBean<List<DapDataModel>> findAllOrFindLikeName(String name) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "模糊获取模型列表");
            }

            @Override
            public ResultBean<Boolean> getBizLineModel(Integer bizId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取模型失败");
            }
        };
    }
}
